import 'package:get/get.dart';
import 'package:shop/tools/searchServices.dart';
import 'package:shop/tools/storage.dart';

class SearchController extends GetxController {
  String keyWords = '';

  RxList searchHistoryList = [].obs;

  @override
  void onInit() {
    super.onInit();
    getSearchHistoryData();
  }

  @override
  void onClose() {
    super.onClose();
  }

  getSearchHistoryData() async {
    var tempList = await SearchServices.getHistoryData();
    if (tempList.isNotEmpty) {
      searchHistoryList.addAll(tempList);
      update();
    }
  }

  clearSearchHistoryData() async {
    searchHistoryList.value = [];
    update();
    SearchServices.clearHistoryData();
  }

  deleteSearchHistoryData(keyWord) async {
    List temList = await SearchServices.getHistoryData();
    if (temList.isNotEmpty) {
      searchHistoryList.remove(keyWord);
      update();
      temList.remove(keyWord);
      await Storage.setData('searchList', temList);
    }
  }
}
